<?php

namespace plugin\laoha\app\common;

use stdClass;
use support\Log;
use Throwable;
use Webman\Event\Event;
/**
 * 用户页面接口
 */
class Template
{

    /**
     * 网页头header
     * @param $title
     * @param array $options
     * @return false|string
     */
    public static function header($title, array $options = [])
    {
        ob_start();
        include base_path('plugin/laoha/app/view/header.html');
        return ob_get_clean();
    }


    /**
     * 通用导航菜单
     *
     * @Author 老蟹 7040867@qq.com    DateTime 2023-11-15
     * @return string|null
     */
    public static function nav($root='default',$active='/')
    {
        if(is_array($root)){ //直接传递navs,可以是空数组
            $navs = $root;
        }else{
            $navs = self::navs($root);
        }
        ob_start();
        try {
            include base_path('plugin/laoha/app/view/nav.html');
        } catch (Throwable $e) {
            Log::error($e);
        }
        return ob_get_clean();
    }

    protected static function navs($root='default')
    {
        $set = [
            'page_type' => 3,
            'with' => '',
            'order_by' => 'parent_id,sort,id',
            'no_where' => 'status',
        ];
        $model = new \plugin\laoha\app\model\Nav();
        $builder = $model->where('status', 0)->where('root', $root);
        $navs = laoha_query($builder, $set, $model);
        return $navs;  
    } 
    //=======结束

    /**
     * 用户导航菜单
     *
     * @Author 老蟹 7040867@qq.com    DateTime 2023-11-15
     * @return string|null
     */
    public static function user_nav($user_id=0,$url='')
    {
        $navs = self::navs('user');
        ob_start();
        try {
            include base_path('plugin/laoha/app/view/user_nav.html');
        } catch (Throwable $e) {
            Log::error($e);
        }
        return ob_get_clean();
    }

    //=======结束    

    /**
     * 面包屑导航
     *
     * @Author 老蟹 7040867@qq.com    DateTime 2023-11-15
     * @param array|string $breadcrumb
     * 格式['标题|url','标题|url',...,'标题'] | '标题|url,标题|url,...,标题'
     * @return string|null
     */
    public static function bread($breadcrumb='')
    {
        if(empty($breadcrumb)) return '';
        ob_start();
        try {
            include base_path('plugin/laoha/app/view/breadcrumb.html');
        } catch (Throwable $e) {
            Log::error($e);
        }
        return ob_get_clean();
    }    
    //========= 面包屑结束

    /**
     * 边栏搜索
     *
     * @Author 老蟹 7040867@qq.com    DateTime 2023-11-15
     * @param array $init
     * @return string|null
     */
    public static function search($url='/app/laoha/post/search')
    {
        ob_start();
        try {
            include base_path('plugin/laoha/app/view/search.html');
        } catch (Throwable $e) {
            Log::error($e);
        }
        return ob_get_clean();
    }    
    //========= 搜索结束


    /**
     * 通用分类列表
     *
     * @Author 老蟹 7040867@qq.com    DateTime 2023-11-15
     * @return string|null
     */
    public static function cate($root='post')
    {
        $cates = self::cates($root);
        $cates = tree2list($cates);

        ob_start();
        try {
            include base_path('plugin/laoha/app/view/cate.html');
        } catch (Throwable $e) {
            Log::error($e);
        }
        return ob_get_clean();
    }

    protected static function cates($root='post')
    {
        $set = [
            'page_type' => 3,
            'with' => '',
            'order_by' => 'parent_id,sort,id',
            'no_where' => 'status,root,is_hidden',
        ];
        $model = new \plugin\laoha\app\model\Cate();
        $builder = $model->where('root', $root)->where('status', 0)->where('is_hidden', 0);
        $data = laoha_query($builder, $set, $model);
        $data = filter_cates($data);
        return $data;  
    }
    //========= 边栏分类结束

    /**
     * 边栏文章列表,非通用
     *
     * @Author 老蟹 7040867@qq.com    DateTime 2023-11-15
     * @param array $init
     * @return string|null
     */
    public static function list($params=[],$image=false)
    {
        $data = self::get_list($params);
        ob_start();
        try {
            include base_path('plugin/laoha/app/view/list.html');
        } catch (Throwable $e) {
            Log::error($e);
        }
        return ob_get_clean();
    }   

    protected static function get_list($params=[]){
        $set = [
            'with' => '',
            'page_type' => 0,
            'select' => 'id,title,cover,created_at',
            'like_where' => 'title,content,pos',
            'status'=>0
        ];
        $set = array_merge($params,$set);
        $model = new \plugin\laoha\app\model\Post();

        $builder = $model->whereHas('cate',function($query){
                $query->where('status',0)->where('is_hidden',0);
            }); 
        
        $data = laoha_query($builder, $set, $model);  
        return $data;
    }
    //========= 列表结束



    /**
     * 内容中幻灯
     *
     * @Author 老蟹 7040867@qq.com    DateTime 2023-11-15
     * @return string|null
     */
    public static function slide($images=[])
    {
        if(is_string($images)){
            $images = str2arr($images);
        }
        ob_start();
        try {
            include base_path('plugin/laoha/app/view/slide.html');
        } catch (Throwable $e) {
            Log::error($e);
        }
        return ob_get_clean();
    }    

    /**
     * 内容中格子图片，一行三张
     *
     * @Author 老蟹 7040867@qq.com    DateTime 2023-11-15
     * @return string|null
     */
    public static function images($images=[])
    {
        if(is_string($images)){
            $images = str2arr($images);
        }
        
        $num = count($images);
        if($num>4){
            if($num%4==0){
                $num=4;
            }else{
                $num=3;
            }
        }
        if($num>2){
            $w = 960/$num;
            $h = 960/$num;
        }elseif($num==2){
            $w = 480;
            $h = 360;
        }else{
            $num=1;
            $w= 960;
            $h= 0;
        }
        ob_start();
        try {
            include base_path('plugin/laoha/app/view/images.html');
        } catch (Throwable $e) {
            Log::error($e);
        }
        return ob_get_clean();
    }       


    /**
     * 分页
     *
     * @Author 老蟹 7040867@qq.com    DateTime 2023-11-15
     * @return string|null
     */
    public static function page($data=[])
    {
        $total= $data['total'] ?? 0;
        $per_page=$data['per_page'] ?? 10;
        ob_start();
        try {
            include base_path('plugin/laoha/app/view/page.html');
        } catch (Throwable $e) {
            Log::error($e);
        }
        return ob_get_clean();
    }  

    
    /**
     * 评论
     *
     * @Author 老蟹 7040867@qq.com    DateTime 2023-11-15
     * @return string|null
     */
    public static function comments($config=[],$id=null)
    {
        if(empty($config['comment']) || empty($config['name']) || empty($id)) return '';
        $is_manage = is_manage();
        $check = !empty($config['comment_check'])?1:0;
        $root_id=$id;
        $root=$config['name'];
        $comments = self::get_comments($config,$id,$is_manage);
        
        ob_start();
        try {
            include base_path('plugin/laoha/app/view/comments.html');
        } catch (Throwable $e) {
            Log::error($e);
        }
        return ob_get_clean();
    }    

    protected static function get_comments($config,$root_id,$is_manage=false){
        $root = $config['name'];
        $commentModel = new \plugin\laoha\app\model\Comment();
        $check = !empty($config['comment_check'])?1:0;
        if($check && !$is_manage ){
            if(user_id()){
                $commentModel = $commentModel->whereNot(function ($query){
                    $query->where('status',0)->where('user_id', '!=', user_id());
                });
            }else{
                $commentModel = $commentModel->where('status',0);
            }
        }
        if($comments = $commentModel->where('root',$root)->where('root_id',$root_id)->with(['user:id,nickname,avatar'])->get()){
            $comments = $comments->toArray();
            return array2tree($comments);
        }else{
            return [];
        }
    }
    //===========评论结束


    /**
     * 网页footer
     * @param array $options
     * @return false|string
     */
    public static function footer(array $options = [])
    {
        ob_start();
        try {
            include base_path('plugin/laoha/app/view/footer.html');
        } catch (Throwable $e) {
            Log::error($e);
        }
        return ob_get_clean();
    }    

    public static function user($user_id)
    {
        $model = new \plugin\user\app\model\User();
        $user = $model->find($user_id);
        ob_start();
        try {
            include base_path('plugin/laoha/app/view/user.html');
        } catch (Throwable $e) {
            Log::error($e);
        }
        return ob_get_clean();
    }    
}